//
//  MySecondPageViewController.m
//  Cook
//
//  Created by Macbookpro on 15/4/25.
//  Copyright (c) 2015年 dingjun. All rights reserved.
//

#import "MySecondPageViewController.h"
#import "FavoriteViewController.h"
#import "VideoDownViewController.h"
#import "RemoveCache.h"
#import "UMSocial.h"
#import "Mycell.h"

@interface MySecondPageViewController ()<UITableViewDataSource, UITableViewDelegate, UMSocialUIDelegate>
{
    CGAdapter _myAd;
    UITableView *_tableView;
    NSTimer *_timer;
    UIAlertView *_cacheAlertView; // 清除缓存提示信息
    

    
}
@property (nonatomic, retain) NSMutableArray *tableArray;
@property (nonatomic, retain) UILabel *cacheLabel;
@property (nonatomic, retain) RemoveCache *remove; // 删除缓存类对象

@end

@implementation MySecondPageViewController

- (void)dealloc
{
    [_tableArray release];
    [_cacheLabel release];
    [_remove release];
    
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.tableArray = [NSMutableArray array];
        self.remove = [[[RemoveCache alloc] init] autorelease];
        [_tableArray addObject:@"我的收藏"];
        [_tableArray addObject:@"视频列表"];
        [_tableArray addObject:@"清理缓存"];
        [_tableArray addObject:@"软件分享"];
     
        _myAd = [AdapterModel getCGAdapter];

    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    self.navigationItem.title = @"我的";
    [self creatBackground];
    [self creatTableView];
}

- (void)setDataSource{
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
   [_tableView reloadData];
}

#pragma mark - 获取缓存文件大小
- (CGFloat)getCacheSize
{
    CGFloat num = [_remove floatWithPath:[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]];
    
    return num;
}

#pragma mark - 创建背景
- (void)creatBackground
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64 * _myAd.sHeight, self.view.frame.size.width, self.view.frame.size.height - 64 * _myAd.sHeight)];
    [imageView setImage:[UIImage imageNamed:@"fourthBarBacground.jpg"]];
    [self.view addSubview:imageView];
    [imageView release];
}

#pragma mark - 创建tableview
- (void)creatTableView
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * 0.3, 64 * _myAd.sHeight + (self.view.frame.size.height - 64 * _myAd.sHeight - 249 * _myAd.sHeight) * 0.2, self.view.frame.size.width * 0.4, (self.view.frame.size.height - 64 * _myAd.sHeight - 249 * _myAd.sHeight) * 0.4)];
    [imageView setImage:[UIImage imageNamed:@"touxiang.png"]];
    [self.view addSubview:imageView];
    [imageView release];
    
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * 0.39, 64 * _myAd.sHeight + (self.view.frame.size.height - 64 * _myAd.sHeight - 249 * _myAd.sHeight) * 0.65, self.view.frame.size.width * 0.5, 30 * _myAd.sHeight)];
    [imageView1 setImage:[UIImage imageNamed:@"kanyikan.png"]];
    [self.view addSubview:imageView1];
    [imageView1 release];
    
    
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 279 * _myAd.sHeight, self.view.frame.size.width, self.view.frame.size.height * _myAd.sHeight) style:UITableViewStylePlain];
    _tableView.separatorInset = UIEdgeInsetsMake(0, 50 * _myAd.sWidth, 0, 0);
    [self.view addSubview:_tableView];
    [_tableView setBackgroundColor:[UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:1.0]];
    
    _tableView.scrollEnabled = NO;
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    [_tableView setRowHeight:40 * _myAd.sHeight];
    [_tableView release];
    
}

#pragma mark - 实现tableview协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentify = @"my";
    
    Mycell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell) {
        cell = [[[Mycell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentify] autorelease];
    }
    
    cell.label.text = [_tableArray objectAtIndex:indexPath.row];
    NSString *my = [NSString stringWithFormat:@"wode%ld", (long)indexPath.row];
    [cell.aImageView setImage:[UIImage imageNamed:my]];
    if (indexPath.row == 2) {
//        if (self.cacheLabel == nil) {
        [self.cacheLabel setText:@""];
        self.cacheLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 170 * _myAd.sWidth, 5 * _myAd.sHeight, 150 * _myAd.sWidth, 35 * _myAd.sHeight)];
        _cacheLabel.textAlignment = NSTextAlignmentRight;
//        }
        CGFloat num = [self getCacheSize];
        NSString *huancun = [NSString stringWithFormat:@"%.2f MB", num];
        
        [_cacheLabel setText:huancun];
        [cell addSubview:_cacheLabel];
        [_cacheLabel release];
        
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        FavoriteViewController *view = [[FavoriteViewController alloc] init];
        [self.navigationController pushViewController:view animated:YES];
        [view release];
    }
    if (indexPath.row == 1) {
        VideoDownViewController *video = [[VideoDownViewController alloc] init];
        [self.navigationController pushViewController:video animated:YES];
        [video release];
    }
    if (indexPath.row == 2) {
        
        CGFloat num = [self getCacheSize];
        _cacheAlertView = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%@, %0.2fMB", @"本次清除缓存", num] message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
        
        [_cacheAlertView show];
        _timer = [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(cacheFinish) userInfo:nil repeats:NO];
        [_remove removeCache];
        [_tableView reloadData];
    }
    if (indexPath.row == 3) {
        UIImage *image = [UIImage imageNamed:@"Default-Port-hd55.png"];
        [UMSocialSnsService presentSnsIconSheetView:self appKey:@"552f7daafd98c59a88000aae" shareText:@"厨房助手助你成为厨神" shareImage:image shareToSnsNames:@[UMShareToSina] delegate:self];
    }
}

// 清除缓存成功提示信息消失方法
- (void)cacheFinish
{
    [_timer invalidate];
    [_cacheAlertView dismissWithClickedButtonIndex:0 animated:YES];
    [_cacheAlertView release];
}


@end
